package com.huluobo.a2307monthdemo2.adapter

import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.huluobo.a2307monthdemo2.R
import com.huluobo.lib_base.bean.MessageEntity

/**
 *  Created by LC on 2025/10/10.
 */
class MessageAdapter : BaseQuickAdapter<MessageEntity, BaseViewHolder>(R.layout.item_chat) {
    override fun convert(holder: BaseViewHolder, item: MessageEntity) {
        val msg = "${item.username}:${item.msg}"
        val builder = SpannableStringBuilder(msg)

        val blue = ForegroundColorSpan(Color.BLUE)
        val red = ForegroundColorSpan(Color.RED)
        val yellow = ForegroundColorSpan(Color.YELLOW)

        if (item.username == "系统") {
            builder.setSpan(red, 0, item.username.length + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
        } else {
            builder.setSpan(blue, 0, item.username.length + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
        }

        //剩余文字标记为黄色
        builder.setSpan(yellow, item.username.length + 1, msg.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
        holder.setText(R.id.item_chat_tv, builder)
    }
}